package com.zhangwei.zk1018.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.zhangwei.zk1018.entity.Goods;
import com.zhangwei.zk1018.entity.GoodsVo;
import com.zhangwei.zk1018.entity.Gtypes;
import com.zhangwei.zk1018.service.IGoodsService;
import com.zhangwei.zk1018.service.IGtypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-10-18
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IGtypesService gtypesService;



    @RequestMapping("list")
    public String list( HttpSession session,Model model, GoodsVo vo,
                       @RequestParam(defaultValue = "1")Integer pageNum,
                       @RequestParam(defaultValue = "3")Integer pageSize){


        System.err.println(vo.getPx());
        Integer px = vo.getPx();
        if (px!=null){
            model.addAttribute("o",Integer.valueOf(px));
        }

        if (px!=null&&px==0){
            px=px;

        }else if(px!=null&&px==1){
            px=px;

        }else if (px!=null){
            px=px%2;
        }

        System.err.println("px="+px);
        vo.setPx(px);


        PageInfo info = goodsService.getList(vo, pageNum, pageSize);

        List<Gtypes> list = gtypesService.list();

        model.addAttribute("info",info);
        model.addAttribute("vo",vo);
        model.addAttribute("list",list);


        return "list";
    }


    @RequestMapping("toAdd")
    public String toAdd(Model model){

        List<Gtypes> gtype = gtypesService.getGtype();
        model.addAttribute("gtype",gtype);
        return "add";
    }

    @RequestMapping("add")
    @ResponseBody
    public boolean add(Goods goods){

        goods.setGtime(new Date()) ;

        boolean save = goodsService.save(goods);
        if (save){
            return true;
        }

        return false;
    }


    @RequestMapping("toUpdate")
    public String toUpdate(Integer id,Model model){

//        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
//        wrapper.eq("gid",id);
        Goods byId = goodsService.getGoodsById(id);

        List<Gtypes> gtype = gtypesService.getGtype();
        for (Gtypes gtypes : gtype) {
            System.err.println(gtypes);
        }

        model.addAttribute("gtype",gtype);
        model.addAttribute("byId",byId);

        return "update";
    }


    @RequestMapping("update")
    @ResponseBody
    public boolean update(Goods goods){

        boolean b = goodsService.saveOrUpdate(goods);
        if (b){
            return  true;
        }

        return false;
    }

    @RequestMapping("delAll")
    @ResponseBody
    public boolean delAll(String gids){

        Integer delAll = goodsService.delAll(gids);

        if (delAll>0){
            return true;
        }


        return false;
    }

    @RequestMapping("ck")
    public  String ck(Model model,Integer id){

        Goods byId = goodsService.getGoodsById(id);
        List<Gtypes> gtype = gtypesService.getGtype();
        model.addAttribute("byId",byId);
        model.addAttribute("gtype",gtype);
        return "ck";
    }



}

